﻿using System;
using System.Drawing.Design;
using System.ComponentModel;
using System.Windows.Forms;
using System.Windows.Forms.Design;


namespace Water.Web.Controls.Design
{
    public class RichTextEditor:UITypeEditor
    {
        public override UITypeEditorEditStyle GetEditStyle(System.ComponentModel.ITypeDescriptorContext context)
        {
            //return base.GetEditStyle(context);
            return UITypeEditorEditStyle.Modal;
        }
        public override object EditValue(System.ComponentModel.ITypeDescriptorContext context, IServiceProvider provider, object value)
        {
            //return base.EditValue(context, provider, value);
            //try
            //{
            //    IWindowsFormsEditorService edSvc = (IWindowsFormsEditorService)
            //    provider.GetService(typeof(IWindowsFormsEditorService));
            //    if (edSvc != null)
            //    {
            //        if (value is string)
            //        {
            //            Form form = new Form();
            //            RichTextBox box = new RichTextBox();
            //            box.Text = value as string;
            //            form.Controls.Add(box);
            //            //edSvc.DropDownControl(box);
            //            edSvc.ShowDialog(form);
            //            return box.Text;
            //        }
            //    }
            //}
            //catch (Exception ex)
            //{
            //    System.Console.WriteLine("RichTextEditor Error : " + ex.Message);
            //    return value;
            //}
            //return value; 

            if (context != null && context.Instance != null && provider != null)
            {
                IWindowsFormsEditorService edSvc = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));

                if (edSvc != null)
                {
                     RichTextEditorForm form;

                    if (value != null && value.ToString() != String.Empty)
                    {
                        form = new RichTextEditorForm(value.ToString());
                    }
                    else
                    {
                        form = new RichTextEditorForm();
                    }

                    if (edSvc.ShowDialog(form) == DialogResult.OK)
                    {
                        return form.RichText;
                    }
                    else
                    {
                        return value;
                    }
                }
                else
                {
                    return value;
                }
            }

            return value;


        }
    }

}
